package com.cxl.parser.grab;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Tag;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasChildFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class GrabAmazon extends GrabCommon implements GrabInfo {

	
	
	public NodeFilter getFilter() {
		return   new AndFilter(new TagNameFilter("b"),
				new  HasAttributeFilter("class", "priceLarge"));
		
	}

	public NodeFilter getPictureFilter() {
		return new AndFilter(new TagNameFilter("img"),
				new HasAttributeFilter("width", "280"));
	}
	
	/**
	 * <div class="data">
    <div class="title">div class="data"
<a class="title" span class="text"  <span class="srTitle">
	 */
	public NodeFilter getSearchUrlFiter() {
		NodeFilter filter4 = new AndFilter(new TagNameFilter("span"),
		new HasAttributeFilter("class", "srTitle"));
		return   new AndFilter(new TagNameFilter("a"),
			new HasChildFilter(filter4));
		
		
//return new TagNameFilter("a");
		
//		NodeFilter filter4 = new AndFilter(new TagNameFilter("div"),
//				new HasAttributeFilter("class", "data"));
		
//		NodeFilter filter2 = new AndFilter(new TagNameFilter("div"),
//				new HasParentFilter(filter4));
//		
//		return   new AndFilter(new TagNameFilter("a"),
//				new  HasParentFilter(filter2));
		
		
//		NodeFilter filter4 = new TagNameFilter("div");
//		NodeFilter filter2 = new AndFilter(new TagNameFilter("div"),
//		new HasParentFilter(filter4));
//
//		return   new AndFilter(new TagNameFilter("a"),
//		new  HasParentFilter(filter2));
//		
		//return filter2;
	}				 


//	@Override
//	public String getEncode() {
//		// TODO Auto-generated method stub
//		return "GB2312";
//	}
	
	@Override
	public NodeFilter getPriceFilter() {
		return   new AndFilter(new TagNameFilter("b"),
				new  HasAttributeFilter("class", "priceLarge"));
	}

						
	public List<String> getOtherImgsHref() throws ParserException{
		String shopId=getShopID();
		String url="http://www.amazon.cn/gp/product/images/"+shopId;
		List <String> imgs=new ArrayList();		
		NodeFilter filter = getOtherPicturesFilter();
		if(filter==null)
		{
			return imgs;
		}
		
		NodeList list = getNodesByFilter(getNodeListHtml(url), filter, -1);
			for(int i=0;i<list.size();i++)
			{
				Node textnode = list.elementAt(i);
				
				Tag a = (Tag) textnode;
			//	System.out.println(a.getAttribute("src"));
				imgs.add(a.getAttribute("src").replace("SL75_SS45","SS400"));
			}
		return imgs;
		
	}

	
	public NodeFilter getNextFilter() {
		return new AndFilter(new TagNameFilter("a"),
				new HasAttributeFilter("class", "pagnNext"));
	}

	//<div class="bucket" id="productDescription">
	public NodeFilter getContentFilter() {
		return   new AndFilter(new TagNameFilter("div"),
				new  HasAttributeFilter("id", "productDescription"));
	}

	public NodeFilter getContentFilter1() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getContentFilter2() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getOtherPicturesFilter()
	{
		NodeFilter filter1 =new AndFilter(new TagNameFilter("td"),
			new  HasAttributeFilter("width", "45"));
		NodeFilter filter2 = new AndFilter(new TagNameFilter("a"),
				new HasParentFilter(filter1));
		NodeFilter filter3 = new AndFilter(new TagNameFilter("img"),
				new HasParentFilter(filter2));
//		
		return filter3;
		//return new TagNameFilter("img");
	
	}

	
	

	public NodeFilter getTitleFilter() {
		NodeFilter filter=new AndFilter(new TagNameFilter("span"),new  HasAttributeFilter("id", "btAsinTitle"));
		return filter;
	}

	public NodeFilter getNotExistsFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	public NodeFilter getBrandFilter() {
		// TODO Auto-generated method stub
		return null;
	}
	
	

	
	
	public NodeFilter getShopIDFilter() {
		NodeFilter filter=new AndFilter(new TagNameFilter("input"),new  HasAttributeFilter("id", "ASIN"));
		return filter;
	}

	public static void main(String[] args) throws ClientProtocolException, IOException {
		//"http://www.amazon.cn/gp/product/images/B002NGNJSY/"
		//<input type="hidden" id="ASIN" name="ASIN" value="B002NGNJSY" />		
		
		String A="jdksajdjddjdjdjjdjd\"dsadklasdkladskal";
		String b="d\"";
		//String url="http://127.0.0.1:10000/?url=http://product.dangdang.com/product.aspx?product_id=410004580";
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpUriRequest request = new HttpGet("http://www.amazon.cn/%E9%A3%9E%E5%88%A9%E6%B5%A6-Philips-2-1%E9%9F%B3%E7%AE%B1-SPA1312/dp/B002NGNJSY/ref=sr_1_1/479-2100300-6465401?ie=UTF8&s=electronics&qid=1313655304&sr=8-1");
		
		//HttpUriRequest request = new HttpGet(url);
	
		HttpResponse response;
		response = httpClient.execute(request);
		HttpEntity entity = response.getEntity();
		String content = EntityUtils.toString(entity);
		
	//	System.out.println(content.indexOf("jpg"));
		
		File file=new File("c:\223.html");
		
		PrintWriter out = new PrintWriter(file);
		out.write(content);
		out.close();

		
		
		
		//System.out.println(content);
		
		
	}

}
